package com.NxIndustries.Sapphire;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.htc.configuration.HtcWrapConfiguration;
import com.htc.preference.HtcPreference;
import com.htc.preference.HtcPreference.OnPreferenceClickListener;
import com.htc.preference.HtcPreferenceActivity;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;

public class Settings extends HtcPreferenceActivity implements OnPreferenceClickListener  {
	private ActionBarExt actionBarExt=null;
	private ActionBarText actionBarText=null;    
	private ActionBarContainer actionBarContainer=null;
	
	private HtcPreference mRestart;
	
	private boolean mFirstRun = true;
	
	private static final String AppTitle = "Welcome Sapphire User";
	
	/*
	 * Check whether the pro app is installed
	 */
	private boolean appInstalledOrNot(String uri) {
		PackageManager pm = getPackageManager();
		boolean app_installed = false;
		try {
			pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
			app_installed = true;
		} catch (PackageManager.NameNotFoundException e) {
			app_installed = false;
		}
		return app_installed;
	}

	/*
	 * Set the initial launch
	 */
	public boolean isFirstRun() {
		return mFirstRun;
	}
	
	public static int getHtcThemeID(Context context, int i)
    {
    	return HtcWrapConfiguration.getHtcThemeId(context, i);
        
    }
	
	private void SetupActionBar() {
		actionBarExt=new ActionBarExt(this,getActionBar());
        actionBarContainer=actionBarExt.getCustomContainer();
        actionBarText=new ActionBarText(this);   
		actionBarText.setPrimaryText(AppTitle);
		actionBarContainer.addCenterView(actionBarText);
		boolean installed = appInstalledOrNot("com.NxIndustries.Sapphire1NE");
			if (installed) {
				actionBarText.setPrimaryText(AppTitle + " 1NE");
			} else {
				actionBarText.setPrimaryText(AppTitle);
			}
		}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
		
		boolean installed = appInstalledOrNot("com.NxIndustries.Sapphire1NE");
		if (installed) {
			System.out.println("Sapphire 1NE user detected!");
		} else {
			System.out.println("Sapphire services started...");
		}
		
		Context context = this;
		setTheme(getHtcThemeID(context, 1));
			addPreferencesFromResource(R.xml.launcher_settings);
			SetupActionBar();
			
			mRestart = findPreference("restart");
            mRestart.setOnPreferenceClickListener(this);
	}
	
	public boolean onPreferenceClick(HtcPreference preference) {
            String key = preference.getKey();

            if (key.equals("restart")) {
                    // Do code here
            	restartApp(this);
                    return true;
            }
            return false;
    }

	private void restartApp(Settings settings) {
		sendBroadcast(new Intent("kill"));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.settings_menu, menu);
      return super.onCreateOptionsMenu(menu);
    } 
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.one:
		@SuppressWarnings("unused")
		final Context context = this;
		Intent one = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.NxIndustries.Sapphire1NE"));
		startActivity(one);
			return true;
      case R.id.good:
  		@SuppressWarnings("unused")
		final Context context1 = this;
  		Intent good = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.NxIndustries.Sapphire"));
		startActivity(good);
	}
	return false;
	}
}
